<?php

//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 商户 代理商
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//

namespace App\Models\Api;

use App\Tools\Financial;
use App\Tools\Prstatic;
use Illuminate\Database\Eloquent\Model;

class StoreGoods extends Model
{
    protected $table = 'store_goods';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳

    /**
     * 获取单条信息
     */
    public static function getInfoById($id, $store_id)
    {
        if ($id && $store_id) {
            $info = self::where(['id' => $id, 'store_id' => $store_id])->first();
            if ($info) {
                return $info->toArray();
            }
        }
        return [];
    }

    /**
     * 获取单条信息
     */
    public static function getInfoByGId($goods_id, $store_id)
    {
        if ($goods_id && $store_id) {
            $info = self::where(['goods_id' => $goods_id, 'store_id' => $store_id])->first();
            if ($info) {
                return $info->toArray();
            }
        }
        return [];
    }

    /**
     * 所有商品商品库存
     * @return mixed
     */
    public static function getRepertoryList($pamas, $limit = 10)
    {
        if ($pamas['store_id']) {
            $where[] = ['sg.store_id', '=', $pamas['store_id']];
        }
        if ($pamas['goods_name']) {
            $where[] = ['g.goods_name', 'like', "%{$pamas['goods_name']}%"];
        }
        if ($pamas['goods_sn']) {
            $where[] = ['g.goods_sn', '=', $pamas['goods_sn']];
        }
        if ($pamas['cate_id']) {
            $where[] = ['g.cate_id', '=', $pamas['cate_id']];
        }
        $list = self::from('store_goods as sg')
            ->select(['g.id', 'sg.goods_id', 'g.goods_sn', 'g.goods_img', 'g.mini_quantity', 'g.goods_name', 'g.goods_price', 'sg.goods_price as sale_price', 'c.cate_name', 'sg.goods_total_num', 'sg.goods_sale_num'])
            ->leftJoin('goods as g', 'g.id', '=', 'sg.goods_id')
            ->leftJoin('goods_category as c', 'c.id', '=', 'g.cate_id')
            ->where($where)
//            ->whereIn('g.id', [82, 83, 84, 85])
            ->orderBy('sg.id', 'desc')
            ->paginate($limit);
        return $list;
    }

}
